/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Khohang;
import util.HibernateUtil;

/**
 *
 * @author HoaDinhGiang
 */
public class khohangDAO {

    public static List<Khohang> layDanhSachKhoHang() {
        List<Khohang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kv from Khohang kv where kv.kichhoat = true ORDER BY kv.makhohang ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static Khohang layThongTinKhoHang(int maHangHoa) {
        Khohang hh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            hh = (Khohang) session.get(Khohang.class, maHangHoa);

        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return hh;
    }

    public static boolean themKhohang(Khohang hh){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction =  session.beginTransaction();
            Serializable save = session.save(hh);
            transaction.commit();
        } catch (HibernateException ex) {
        //Log the exception
            transaction.rollback();
            System.err.println(ex);
            return false;
        } finally {
        session.close();
        }
        return true;
    }

    public static boolean capNhatThongTinKhoHang(Khohang hh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(hh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
    }

    public static void xoaTamThoi(Khohang hh) {
        /*
         * Session session = HibernateUtil.getSessionFactory().openSession();
         * try{ String hql = "update Cuahang ch set kichhoat = false where
         * ch.macuahang = "+maCuaHang; System.out.println(hql); Query query =
         * session.createQuery(hql); int k = query.executeUpdate();
         * System.out.print(k); }catch(HibernateException ex){ //Log the
         * exception System.err.println(ex); }finally{ session.close(); }
         *
         */
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(hh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
    }
}
